package com.sign2024.sign2024project.projectservice.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sign2024.sign2024core.common.CommonResult;
import com.sign2024.sign2024core.common.annotation.Log;
import com.sign2024.sign2024core.common.constants.CommonConstants;
import com.sign2024.sign2024core.common.exeception.CommonException;
import com.sign2024.sign2024core.common.utils.ExcelStyleUtils;
import com.sign2024.sign2024core.entry.projectservice.SpecialtySignRelation;
import com.sign2024.sign2024project.projectservice.param.RelationParam;
import com.sign2024.sign2024project.projectservice.service.SpecialtySignRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * 专家专业与标段对应关系 控制层
 *
 * @author yujie
 * @data 2024/8/1 9:53
 */
@Api(tags = "工程服务")
@RestController
@RequestMapping("/api/specialtySignRelation")
public class SpecialtySignRelationController {

    @Resource
    private SpecialtySignRelationService specialtySignRelationService;


    @Log(title = "工程服务", businessType = "导入")
    @ApiOperation(value = "专家专业与标段对应关系excel导入", notes = "专家专业与标段对应关系excel导入")
    @Transactional
    @PostMapping("/import")
    public CommonResult<SpecialtySignRelation> importInfo(MultipartFile file) {
        // 这里的导入 是后台前期把数据放到历史版本中的数据
        // 调用表中的 的清空
//        specialtySignRelationService.cleanSpecialtySignRelation();

//        SpecialtySignRelationExcelListener listener = new SpecialtySignRelationExcelListener();
        try {
            // 获取上传文件的名称
            String fileName = file.getOriginalFilename();

//            EasyExcel.read(file.getInputStream(), listener).head(SpecialtySignRelation.class).sheet().doReadSync();
            List<SpecialtySignRelation> list = EasyExcel.read(file.getInputStream()).head(SpecialtySignRelation.class).sheet().doReadSync();

            // 导入成功 添加历史版本数据
            specialtySignRelationService.addHistorical(list, fileName);

            return CommonResult.success();
        } catch (IOException e) {
            e.printStackTrace();
            throw new CommonException(CommonConstants.ERROR, "excel导入失败");
        }
    }


    @Log(title = "工程服务", businessType = "导出")
    @ApiOperation(value = "工程标段与专业对应关系excel导出", notes = "工程标段与专业对应关系excel导出")
    @PostMapping("/export")
//    public void exportInfo(HttpServletResponse response, RelationParam param) {
    public void exportInfo(HttpServletResponse response) {
        List<SpecialtySignRelation> list = specialtySignRelationService.selectAllList();

//        List<SpecialtySignRelation> list = specialtySignRelationService.exportInfoList(param);

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = null;
        try {
            fileName = URLEncoder.encode("专家专业与标段对应关系", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            ServletOutputStream outputStream = response.getOutputStream();

            HorizontalCellStyleStrategy styleBorder =
                    ExcelStyleUtils.createStyleBorder((short) 10, "宋体", (short) 10, "宋体");

            EasyExcel.write(outputStream, SpecialtySignRelation.class)
                    .registerWriteHandler(styleBorder)
                    .sheet("专家专业与标段对应关系").doWrite(list);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CommonException(CommonConstants.ERROR, "专家专业与标段对应关系导出失败");
        }
    }


    @Log(title = "工程服务", businessType = "其他")
    @ApiOperation(value = "工程服务专家对应关系数据历史回退", notes = "工程服务专家对应关系数据历史回退")
    @Transactional
    @PostMapping("/rollback")
    public CommonResult rollback() {
        specialtySignRelationService.rollbackHistorical();
        return CommonResult.success();
    }

    @Log(title = "工程服务", businessType = "查询")
    @ApiOperation(value = "分页查询标段对应关系数据", notes = "分页查询标段对应关系数据")
    @PostMapping("/page")
    public CommonResult<SpecialtySignRelation> page(RelationParam param) {
        Page<SpecialtySignRelation> pageList = specialtySignRelationService.pageSpecialtySignRelation(param);
        return CommonResult.success(pageList);
    }

    @Log(title = "工程服务", businessType = "更新")
    @ApiOperation(value = "修改标段对应关系数据", notes = "修改标段对应关系数据")
    @PostMapping("/update")
    public CommonResult<SpecialtySignRelation> update(SpecialtySignRelation relation) {
        return CommonResult.success(specialtySignRelationService.updateSpecialtySignRelation(relation));
    }
}
